// Author: wsfuyibing <682805@qq.com>
// Date: 2025-05-23

package post

import (
	"context"
	"fmt"
	"gitee.com/go-libs/result"
	"gitee.com/go-libs/validator-iris/validator"
	"gitee.com/go-wares/framework-iris/framework/src/logics"
	"github.com/kataras/iris/v12"
	"server/app/errs"
	"server/app/http/models"
	"server/app/http/services"
	"server/app/kind"
	"server/app/lib"
	"strings"
)

// Logic
// 保存会员设置.
type Logic struct {
	admin  *models.ArcadeAdmins
	arcade *models.Arcades

	affects     int64
	kind        kind.ArcadeAdminOperation
	kindContent string
	setting     *models.ArcadeSetting

	request  *Request
	response *Response
}

// New
// 创建逻辑实例.
func New() logics.Logical {
	return &Logic{
		request:  &Request{},
		response: &Response{},
	}
}

// After
// 后置日志.
func (o *Logic) After(ctx context.Context, _ iris.Context, _ result.Result) {
	if o.affects > 0 {
		_, _ = services.NewArcadeLogsService().Add(ctx,
			o.arcade, o.admin, o.kind, o.kindContent,
		)
	}
}

// Before
// 前置校验.
func (o *Logic) Before(_ context.Context, i iris.Context) (err error) {
	if err = validator.Json(i, o.request); err == nil {
		o.arcade, o.admin = lib.Arcade.GetMaster().GetArcade(i)
	}
	return
}

// Run
// 执行逻辑.
func (o *Logic) Run(ctx context.Context, _ iris.Context) result.Result {
	var (
		err     error
		has     bool
		res     = result.New()
		service = services.NewArcadeSettingService()
	)

	// 1. 读取设置.
	if o.setting, has, err = service.Dao.GetById(ctx, o.arcade.Id); err != nil {
		return res.WithError(errs.ErrDatabaseQuery)
	}

	// 2. 无效设置.
	if !has {
		return res.WithError(errs.ErrArcadeSettingNotFound)
	}

	// 3. 更新设置.
	if data, cols := o.RunCompare(); len(data) > 0 {
		o.kind = kind.ArcadeAdminOperationMemberSetting
		o.kindContent = o.kind.ToMemberSetting(strings.Join(cols, ","))

		if o.affects, err = service.Dao.UpdateFieldsById(ctx, data, o.arcade.Id); err != nil {
			return res.WithError(errs.ErrDatabaseQuery)
		}
	}

	// 3. 设置结果.
	return res.With(o.response.With(o.setting))
}

func (o *Logic) RunCompare() (data map[string]any, cols []string) {
	data = make(map[string]any)
	cols = make([]string, 0)

	// 1. 快人一步.
	if o.request.MemberEarlyEnabled != o.setting.MemberEarlyEnabled {
		data["member_early_enabled"] = o.request.MemberEarlyEnabled
		cols = append(cols, fmt.Sprintf(`快人一步[%s]`, o.request.MemberEarlyEnabled.Text()))
	}
	if o.request.MemberEarlyHours != o.setting.MemberEarlyHours {
		data["member_early_hours"] = o.request.MemberEarlyHours
		cols = append(cols, fmt.Sprintf(`提前时长[%d]`, o.request.MemberEarlyHours))
	}
	// 2. 商店折扣.
	if o.request.MemberGoodsEnabled != o.setting.MemberGoodsEnabled {
		data["member_goods_enabled"] = o.request.MemberGoodsEnabled
		cols = append(cols, fmt.Sprintf(`商店折扣[%s]`, o.request.MemberGoodsEnabled.Text()))
	}
	if o.request.MemberGoodsDiscountType != o.setting.MemberGoodsDiscountType {
		data["member_goods_discount_type"] = o.request.MemberGoodsDiscountType
		cols = append(cols, fmt.Sprintf(`商店折扣类型[%s]`, o.request.MemberGoodsDiscountType.Text()))
	}
	if o.request.MemberGoodsDiscountAmount != o.setting.MemberGoodsDiscountAmount {
		data["member_goods_discount_amount"] = o.request.MemberGoodsDiscountAmount
		cols = append(cols, fmt.Sprintf(`商店折扣值[%f]`, o.request.MemberGoodsDiscountAmount))
	}
	// 3. 高价回鱼.
	if o.request.MemberReturnEnabled != o.setting.MemberReturnEnabled {
		data["member_return_enabled"] = o.request.MemberReturnEnabled
		cols = append(cols, fmt.Sprintf(`高价回鱼[%s]`, o.request.MemberReturnEnabled.Text()))
	}
	if o.request.MemberReturnWithCount != o.setting.MemberReturnWithCount {
		data["member_return_with_count"] = o.request.MemberReturnWithCount
		cols = append(cols, fmt.Sprintf(`按条回鱼[%f]`, o.request.MemberReturnWithCount))
	}
	if o.request.MemberReturnWithWeight != o.setting.MemberReturnWithWeight {
		data["member_return_with_weight"] = o.request.MemberReturnWithWeight
		cols = append(cols, fmt.Sprintf(`按重回鱼[%f]`, o.request.MemberReturnWithWeight))
	}
	// 4. 鱼票折扣.
	if o.request.MemberTicketEnabled != o.setting.MemberTicketEnabled {
		data["member_ticket_enabled"] = o.request.MemberTicketEnabled
		cols = append(cols, fmt.Sprintf(`鱼票折扣[%s]`, o.request.MemberTicketEnabled.Text()))
	}
	if o.request.MemberTicketDiscountType != o.setting.MemberTicketDiscountType {
		data["member_ticket_discount_type"] = o.request.MemberTicketDiscountType
		cols = append(cols, fmt.Sprintf(`鱼票折扣类型[%s]`, o.request.MemberTicketDiscountType.Text()))
	}
	if o.request.MemberTicketDiscountAmount != o.setting.MemberTicketDiscountAmount {
		data["member_ticket_discount_amount"] = o.request.MemberTicketDiscountAmount
		cols = append(cols, fmt.Sprintf(`鱼票折扣值[%f]`, o.request.MemberTicketDiscountAmount))
	}
	return
}
